package com.google.code.sfa.parse;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.lang.Iterable;

public class TraceDatabase implements Iterable<Trace>{
    
    private HashMap<String, Trace> _clones;
    private List<Trace> _traces;

    public TraceDatabase() {
	_clones = new HashMap< String, Trace>();
	_traces = new ArrayList< Trace>();
    }

    public void addTrace( Trace trace) {
	if( trace == null) {
	    return;
	}

	_traces.add( trace);
	if( _clones.containsKey( trace.getCloneId() ) ) {
	    Trace match = _clones.get( trace.getCloneId() );
	    if( match.getForward() != trace.getForward() ) {
		match.setMate( trace);
		trace.setMate( match);
		if( !match.getForward() && trace.getForward() ) {
		    _clones.put( trace.getCloneId(), trace);
		}
	    }
	} else {
	    _clones.put( trace.getCloneId(), trace);
	}
    }

    public List<Trace> toList() {
	return _traces;
    }

    public Iterator<Trace> iterator() {
	return _traces.iterator();
    }
}
		